package com.bite.spring.demo.sample;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-11-04
 * Time: 21:05
 */

//utils 工具类

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String passWord, HttpSession session){
        //参数校验
        /*if(userName == null || userName == "" || passWord == null || passWord == ""){
            return false;
        }*/
        //Stringutils，string工具类，haslength方法，输入的字符串不为空或者null
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(passWord)){
            return false;
        }
        //检验用户和密码是否正确
        //还没学习数据库相关操作，暂且把账号和密码写死，admin，admin
        //equals比较字符串，常量写前面
        if("admin".equals(userName) && "admin".equals(passWord)){
            //密码正确
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }

    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session){
        String userName = (String)session.getAttribute("userName");
        return userName;
    }


}
